#!/bin/bash
# jimgutil native 库arm linux 交叉编译脚本
# author guyadong

# 检测是否安装交叉编译器,没有安装则报错退出
[ -z "$(which arm-linux-gnueabihf-gcc)" ] && echo "ERROR:arm-linux-gnueabihf-gcc not install" && exit 255
[ -z "$(which arm-linux-gnueabihf-g++)" ] && echo "ERROR:arm-linux-gnueabihf-g++ not install" && exit 255

sh_folder=$(dirname $(readlink -f $0))
folder_name=$(basename $sh_folder) 
# 定义编译的版本类型(DEBUG|RELEASE)
build_type=RELEASE
typeset -u arg1=$1
[ "$arg1" = "DEBUG" ] && build_type=$arg1
echo build_type=$build_type

machine=$(arm-linux-gnueabihf-g++ -dumpmachine)

pushd $sh_folder/.. >/dev/null

[ -d $folder_name.armlinux.prj ] && rm -fr $folder_name.armlinux.prj
mkdir $folder_name.armlinux.prj

pushd $folder_name.armlinux.prj >/dev/null

install_prefix=$sh_folder/release/jimgutil_$machine
res_folder=$sh_folder/../jimgutil/src/main/resources/linux-arm/

optsimd=
lib_suffix=
[ -n "$OPTIMIZE_SIMD" ] && optsimd="-DARCH_SIMD=$OPTIMIZE_SIMD" && lib_suffix="_$OPTIMIZE_SIMD"
cmake "$sh_folder" -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=$build_type \
	-DCMAKE_INSTALL_PREFIX=$install_prefix $optsimd \
	-DCMAKE_TOOLCHAIN_FILE=$sh_folder/cmake/toolchain-arm-linux-gnueabihf.cmake

cmake --build . --target install/strip --config release -- -j8 \
	&& mkdir -p $res_folder \
	&& cp -afv $install_prefix/lib/libjimgutil$lib_suffix.so $res_folder \
	&& chmod +x $res_folder/libjimgutil$lib_suffix.so

popd >/dev/null

popd >/dev/null
